//
//  ANnimationController.m
//  DeepBreathing
//
//  Created by rimi on 15/12/28.
//  Copyright © 2015年 rimi. All rights reserved.
//

#import "ANnimationController.h"

@implementation ANnimationController{
    
    UINavigationControllerOperation _operation;
}
- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                            animationControllerForOperation:(UINavigationControllerOperation)operation
                                                         fromViewController:(UIViewController *)fromVC
                                                           toViewController:(UIViewController *)toVC  NS_AVAILABLE_IOS(7_0){
    
    return self;
    
    
    
}
-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
    return 0.8;
}

-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
    
    //实现转场动画
    
    
    //1.获取containView
    UIView *containView = [transitionContext containerView];
//    //2.获取参与转场的视图
//    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    
    UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    UIView * toView =  [transitionContext viewForKey:UITransitionContextToViewKey];
    
    //毛玻璃
//    UIVisualEffect
    
    //把from和to加到conrtrollerView的层级上
    
    if (_operation == UINavigationControllerOperationPush   ) {
        //push : to 盖住from
        [containView addSubview:fromView];
        [containView addSubview:toView];
        toView.frame = CGRectMake(- 375, 0, 375, 667);
        CGFloat duration = [self transitionDuration:transitionContext];
        [UIView animateWithDuration:duration animations:^{
            toView.frame = fromView.frame;
            fromView.frame = CGRectOffset(fromView.frame, 375.f /2,0);
            
        } completion:^(BOOL finished) {
            //指定转场结束
            [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
        }];
        
    }else{
        
        toView.frame = CGRectMake(375, 0, 375, 667);
        CGFloat duration = [self transitionDuration:transitionContext];
        [UIView animateWithDuration:duration animations:^{
            toView.frame = fromView.frame;
            fromView.frame = CGRectOffset(fromView.frame, -375/2,0);
            
        } completion:^(BOOL finished) {
            //指定转场结束
            [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
        }];
        

        [containView addSubview:toView];
        [containView addSubview:fromView];
    }
    
    
    
}
@end
